<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" class="input-box" v-model="value"></el-input>
      <el-button>清除</el-button>
      <el-button type="primary" @click="serchTesk">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" label="任务编号" width="80"> </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="editTesk(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="delTesk(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger" @click="del">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :page-size="params._limit"
        :total="total"
        @current-change="pageChange"
      >
      </el-pagination>
      <el-button size="small" plain @click="addTesk">+ 新增</el-button>
    </div>
    <el-dialog title="提示" :visible="dialogVisible" width="30%" @close="closeDialog">
      <template #default>
        <el-form ref="addForm" :model="teskForm" :rules="rules" label-width="100px">
          <el-form-item label="任务名称" prop="name">
            <el-input v-model="teskForm.name" />
          </el-form-item>
          <el-form-item label="任务描述" prop="des">
            <el-input v-model="teskForm.des" />
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span class="dialog-footer">
          <el-button>取 消</el-button>
          <el-button type="primary" @click="confirm">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchGetList,
  fetchAddTesk,
  fetchDelTesk,
  fetchDetail,
  fetchUpdateTesk,
  fetchSearch
} from '@/api/request.js'
export default {
  data() {
    return {
      list: [],
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      value: '',
      total: 0,
      teskForm: {
        name: '',
        des: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '任务名称必填', trigger: 'blur' }],
        des: [{ required: true, message: '任务描述必填', trigger: 'blur' }]
      },
      dialogVisible: false
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 获取列表
    async getList() {
      const res = await fetchGetList(this.params)
      console.log(res)
      this.list = res.data
      this.total = Number(res.headers['x-total-count'])
    },
    // 分页
    pageChange(page) {
      console.log(page)
      this.params._page = page
      this.getList()
    },
    // 新增任务
    addTesk() {
      this.dialogVisible = true
    },
    // 关闭弹窗
    closeDialog() {
      this.dialogVisible = false
    },
    // 新增or编辑
    confirm() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          if (this.teskForm.id) {
            await fetchUpdateTesk(this.teskForm)
          } else {
            await fetchAddTesk(this.teskForm)
          }
          this.$message.success(`${this.teskForm.id ? '编辑' : '新增'}成功`)
          this.getList()
          this.closeDialog()
          this.teskForm = {
            name: '',
            des: '',
            id: ''
          }
        }
      })
    },
    // 删除功能
    async delTesk(id) {
      // console.log(id)
      await fetchDelTesk(id)
      this.$message.success('删除成功')
      this.getList()
    },
    // 编辑回显
    async editTesk(id) {
      this.dialogVisible = true
      const res = await fetchDetail(id)
      this.teskForm = res.data
    },
    // 搜索功能
    async serchTesk() {
      const res = await fetchSearch(this.value)
      // console.log(res)
      this.list = res.data
    },
    del() {
      console.log('删除')
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
